<?php
	session_start();
	
	$conn = mysql_connect("", "piedu_user", "q&&^%@8ezO8+");
	mysql_select_db("piedu_db");
	
	//Get the promotions
	$promotions = array();
	$sql = "SELECT * FROM promotions WHERE start_date <= now() AND end_date >= now()";
	$promotions_query = mysql_query($sql, $conn);
	if(mysql_num_rows($promotions_query) > 0){
		while($row = mysql_fetch_assoc($promotions_query)){
			$promotions[] = $row;
		}
	} else {
		$promotions = null;
	}
	
	$sql = "SELECT id, sku, description, price, category FROM items";
	if(array_key_exists("category", $_GET)){
		$sql .= " AND category = '{$_GET['category']}' LIMIT 12";
	} else {
		$sql .= " LIMIT 12";
	}
	$items_query = mysql_query($sql, $conn);
	$items = array();
	while($row = mysql_fetch_assoc($items_query)){
		$row['promotion'] = 0;
		$row['original_price'] = $row['price'];
		if($promotions != null){
			foreach($promotions as $promo){
				$array_items = explode(",", $promo['items']);
				if(count($array_items) > 0){
					foreach($array_items as $value){
						if($value == $row['sku']){
							if($promo['discount_type'] == "USD"){
								$row['promotion'] += $promo['discount_amount'];
								$row['price'] -= $promo['discount_amount'];
							} else {
								$row['price'] -= ($promo['discount_amount']/100)*$row['price'];
								$row['promotion'] += ($promo['discount_amount']/100)*$row['price'];
							}
						}
					} //End foreach
				}
				if($promo['promotion_type'] == "general"){
					if($promo['discount_type'] == "USD"){
						$row['price'] -= $promo['discount_amount'];
						$row['promotion'] += $promo['discount_amount'];
					} else {
						$row['price'] -= ($promo['discount_amount']/100)*$row['price'];
						$row['promotion'] += ($promo['discount_amount']/100)*$row['price'];
					}
				} else if($promo['promotion_type'] == $row['category']){
					if($promo['discount_type'] == "USD"){
						$row['price'] -= $promo['discount_amount'];
						$row['promotion'] += $promo['discount_amount'];
					} else {
						$row['price'] -= ($promo['discount_amount']/100)*$row['price'];
						$row['promotion'] += ($promo['discount_amount']/100)*$row['price'];
					}
				}
			} //End foreach
			$items[] = $row;
		} else {
			$items[] = $row;
		}
	} //End while
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Pie Education | Marketing</title>
<Meta name="Keywords" content="education, course, learning, online, virtual, e-learning, school, student, teacher, homework, educational platform, educational system, free tool">
<Meta name="Description" content="Pie Education is a internet based Course Management System (CMS)">
<Meta name="Author" content="Rob Keller">
<Meta name="Publisher" content="Aaron Aceves">
<Meta name="Copyright" content="Copyright© 2012, Pie Education">
<Meta name="Revisit-After" content="30 days">
<Meta HTTP-EQUIV="Expires" content="none">
<Meta name="Pagetopic" content="Education">
<Meta name="Audience" content=" All">
<Meta name="Robots" content="INDEX,FOLLOW">
<Meta name="Content-Language" content="English">
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
<link rel="stylesheet" href="css/style.css" />
</head>

<body>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="90" bgcolor="#FF8C00" align="center"><table width="980" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td><a href="index.php"><img src="images/logo.png" alt="Return to Index" width="235" height="63" border="0" title="pie.edu an E-Learning Network" /></a></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td height="500" bgcolor="#FFFFFF"><table width="98%" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td width="250" valign="top"><table width="95%" border="0" align="center" cellpadding="4" cellspacing="2">
          <tr>
            <td height="10">&nbsp;</td>
          </tr>
          <tr>
            <td height="30"><strong>System Add-Ons</strong></td>
          </tr>
          <tr>
            <td height="30"><strong>Blog System</strong></td>
          </tr>
          <tr>
            <td height="30"><strong>Another Product</strong></td>
          </tr>
          <tr>
            <td height="30"><strong>Another Product</strong></td>
          </tr>
          <tr>
            <td height="30"><strong>A very good product</strong></td>
          </tr>
          <tr>
            <td height="30"><strong>Newest Products</strong></td>
          </tr>
          <tr>
            <td height="30"><strong>Another Product</strong></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
          </tr>
        </table></td>
        <td><table width="100%" border="0" align="center" cellpadding="4" cellspacing="3">
		<tr>
			<td height="10">&nbsp;</td>
        </tr>
          <?php
			$contador = 0;
			foreach($items as $item){
				if($contador == 0){
				  echo '<tr>';
				} else if(($contador % 4) == 0){
				   echo '</tr>
				   <tr>';
				}
				echo '<td width="20%" align="center"><img src="images/sku/'.$item['sku'].'.jpg" width="160" height="149" alt="'.$item['description'].'" title="'.$item['description'].'" />
				<div><strong>'.$item['description'].'</strong></div>';
				  if($item['promotion'] > 0){
					echo '<div style="font-size:9px;color:#999">Regular Price - $'.$item['original_price'].'</div>
					<div style="font-size:10px;color:#0C3">You Save - $'.$item['promotion'].'</div>';
				  }
				  echo '<div style="font-size:11px;color:#F60">Your Price - $'.$item['price'].'</div>
				  <div align="center"><a href="basket.php?item='.$item['id'].'"><img src="images/add_to_cart_icon.gif" alt="Add to my cart" width="95" height="16" border="0" title="Add to my cart" /></a></div>';
				$contador++;
				if($contador >= count($items)){
					echo '</tr>';
				}
			}
          ?>
		  <tr>
			<td height="10">&nbsp;</td>
        </tr>
        </table></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td height="80" bgcolor="#FF9933" align="center"><a href="index.php" class="bottom-general-menu" style="color:#FFF">Home</a> <a href="platform.php" class="bottom-general-menu" style="color:#FFF">Platform</a> <a href="piephone.php" class="bottom-general-menu" style="color:#FFF">Pie Phone</a> <a href="company_training.php" class="bottom-general-menu" style="color:#FFF">Company Training</a> <a href="jobs.php" class="bottom-general-menu" style="color:#FFF">Jobs</a> <a href="press.php" class="bottom-general-menu" style="color:#FFF">Press</a> <a href="community/" class="bottom-general-menu" style="color:#FFF">Community</a> <a href="mobile.php" class="bottom-general-menu" style="color:#FFF">Mobile</a> <a href="support/" class="bottom-general-menu" style="color:#FFF">Help</a> <a href="blog/" class="bottom-general-menu" style="color:#FFF">Blogs</a> <a href="forums/" class="bottom-general-menu" style="color:#FFF">Forums</a><a href="about.php" class="bottom-general-menu" style="color:#FFF">About</a> <a href="terms.html" class="bottom-general-menu" style="color:#FFF">Terms of Use</a> <a href="privacy.html" class="bottom-general-menu" style="color:#FFF">Privacy Policy</a> <a href="contact-us.php" class="bottom-general-menu" style="color:#FFF">Contact Us</a></td>
  </tr>
</table>
</body>
</html>
